package com.zaozhuang.robot;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class SettingsActivity extends Activity {

    private EditText etWakeWord, etWelcomeMessage;
    private Button btnSave;
    private SharedPreferences sharedPreferences;

    // 定义存储键名
    public static final String PREFS_NAME = "AppSettings";
    public static final String KEY_WAKE_WORD = "wake_word";
    public static final String KEY_WELCOME_MSG = "welcome_message";

    public static final String wake_default = "你好，社小保";
    public static final String welcome_default = "各位领导：欢迎莅临“枣工快道”就业服务综合体指导！“榴住人才，职达未来”我是您的向导“社小保”。";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        // 初始化视图
        etWakeWord = findViewById(R.id.et_wake_word);
        etWelcomeMessage = findViewById(R.id.et_welcome_message);
        btnSave = findViewById(R.id.btn_save);

        // 初始化 SharedPreferences
        sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

        // 加载已保存的数据
        loadSavedSettings();

        // 保存按钮点击事件
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveSettings();
            }
        });
    }

    private void loadSavedSettings() {
        // 从 SharedPreferences 读取数据并显示
        String savedWakeWord = sharedPreferences.getString(KEY_WAKE_WORD, wake_default);
        String savedWelcomeMsg = sharedPreferences.getString(KEY_WELCOME_MSG, welcome_default);

        etWakeWord.setText(savedWakeWord);
        etWelcomeMessage.setText(savedWelcomeMsg);
    }

    private void saveSettings() {
        // 获取输入内容
        String wakeWord = etWakeWord.getText().toString().trim();
        String welcomeMsg = etWelcomeMessage.getText().toString().trim();

        // 保存到 SharedPreferences
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(KEY_WAKE_WORD, wakeWord);
        editor.putString(KEY_WELCOME_MSG, welcomeMsg);
        editor.apply();

        // 提示保存成功
        Toast.makeText(this, "设置已保存", Toast.LENGTH_SHORT).show();

        // 刷新显示（可选）
        loadSavedSettings();
    }
}
